import time
import c
current_milli_time = lambda: int(round(time.time() * 1000))

start_time = current_milli_time()

c.setup()

LIMIT = 1000000

def getprimes(limit):
	primes = set()
	nums = range(2,limit+1)
	marked = [0] * limit
	p = 2
	for p in nums:
		if not marked[p-2]:
			n = 2
			a = p*n
			while(a <= limit):
				marked[a-2] = 1
				n = n + 1
				a = p*n
	for x in range(limit-1):
		if not marked[x]:
			primes.add(nums[x])
	return primes

primes = getprimes(LIMIT)

def checkprime(num):
	if num in primes:
		return True
	else:
		return False


def checkrot(num):
	n = [int(str(num)[a:]+str(num)[:a]) for a in range(len(str(num)))]
	for x in n:
		if checkprime(x) is False:
			return False
	return True
total = 0
for x in primes:
	c.progress(x)
	if checkrot(x):
		total = total + 1

c.answer(total,start_time)
c.end()